/**
 * @(#)ApkConfigController.java 2016年12月1日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.annotation.SystemControllerLog;
import org.jsbd.boss.common.ExportExcel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.common.ReturnJsonCode;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.domian.ChannelProvinceInfo;
import org.jsbd.boss.domian.InterceptInfo;
import org.jsbd.boss.domian.ProvinceInfo;
import org.jsbd.boss.domian.UpgradeApkBean;
import org.jsbd.boss.domian.channel.ApkDevice;
import org.jsbd.boss.domian.channel.ApkLoginTypeConfig;
import org.jsbd.boss.domian.channel.ApkRetentConfig;
import org.jsbd.boss.domian.channel.ApkSimulator;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.domian.channel.FileUploadVo;
import org.jsbd.boss.domian.channel.RetentFilterConfig;
import org.jsbd.boss.service.IApkLoginTypeService;
import org.jsbd.boss.service.IApkProvinceService;
import org.jsbd.boss.service.IApkSimulatorService;
import org.jsbd.boss.service.IApkTaskQueryService;
import org.jsbd.boss.service.IChannelApkInfoService;
import org.jsbd.boss.service.IDeviceService;
import org.jsbd.boss.service.IIpService;
import org.jsbd.boss.service.IRetentFilterConfigService;
import org.jsbd.boss.service.IUpgradeApkService;
import org.jsbd.boss.service.impl.ChannelConfigService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import edu.hziee.common.lang.DateUtil;

/**
 * apk配置
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/apkconfig")
public class ApkConfigController {
    public static final Logger LOGGER = LoggerFactory.getLogger(ApkConfigController.class);

    @Resource
    private IChannelApkInfoService channelApkService;

    @Resource
    private ChannelConfigService channelConfigService;
    @Resource
    private IApkProvinceService apkProvinceService;
    @Resource
    private IApkLoginTypeService apkLoginTypeService;

    @Resource
    private IApkSimulatorService apkSimulatorService;

    @Resource
    private IApkTaskQueryService apkTaskQueryService;

    @Resource
    private IIpService ipService;

    @Resource
    private IRetentFilterConfigService retentFilterConfigService;

    @Resource
    private IDeviceService deviceService;
    
    @Resource
    private IUpgradeApkService upgradeApkService;
    
    @Resource
    private RedisClient redisClient;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list() {
        return "/apkconfig/configList";
    }

    @RequestMapping(value = "addConfig", method = RequestMethod.GET)
    public String add() {
        return "/apkconfig/add";
    }

    @RequestMapping(value = "del")
    @SystemControllerLog(description = "删除mark")
    public void del(String mark, HttpServletResponse response) {
        try {
            channelApkService.delApkConfig(mark);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("删除apk出错", e);
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "effect")
    @SystemControllerLog(description = "生效")
    public void effect(String mark, HttpServletResponse response) {
        try {
            ChannelApkConfig apkConfig = channelApkService.selectApkConfig(mark);
            apkConfig.setMark(mark);
            apkConfig.setIsDelete("0");
            channelApkService.updateApkConfig(apkConfig);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("生效apk出错", e);
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "abandon")
    @SystemControllerLog(description = "作废")
    public void abandon(String mark, HttpServletResponse response) {
        try {
            ChannelApkConfig apkConfig = channelApkService.selectApkConfig(mark);
            apkConfig.setIsDelete("1");
            channelApkService.updateApkConfig(apkConfig);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("废弃apk出错", e);
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "saveConfig", method = RequestMethod.POST)
    @SystemControllerLog(description = "新增配置")
    public void saveConfig(ChannelApkConfig channelApkConfig, @RequestParam MultipartFile[] file, HttpServletResponse response) {
        try {
            channelApkService.saveApkConfig(file, channelApkConfig);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("保存apk出错", e);
            ResponseUtils.responseInfoExists(response);
        }
    }

    @RequestMapping(value = "saveCopyConfig", method = RequestMethod.POST)
    @SystemControllerLog(description = "新增配置")
    public void saveCopyConfig(ChannelApkConfig channelApkConfig, @RequestParam MultipartFile[] file, HttpServletResponse response) {
        try {
            channelApkService.saveCopyApkConfig(file, channelApkConfig);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("保存apk出错", e);
            ResponseUtils.responseInfoExists(response);
        }
    }

    @RequestMapping(value = "updateConfig", method = RequestMethod.POST)
    @SystemControllerLog(description = "更新配置")
    public void updateConfig(ChannelApkConfig apkConfig, @RequestParam MultipartFile[] file, HttpServletResponse response) {
        try {
            channelApkService.updateApkConfig(file, apkConfig);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("修改apk出错", e);
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "{mark}/configUpdate")
    public String configUpdate(@PathVariable String mark, Model model) {
        ChannelApkConfig apkConfig = channelApkService.selectApkConfig(mark);
        model.addAttribute("config", apkConfig);
        List<ApkSimulator> vers = apkSimulatorService.getVers(mark);
        model.addAttribute("sysVerList", vers);
        List<InterceptInfo> interceptList = channelConfigService.getInterceptInfoByChannelApkInfo(apkConfig);
        model.addAttribute("interceptList", interceptList);

        return "apkconfig/updateConfig";
    }

    @RequestMapping(value = "{mark}/configCopy")
    public String configCopy(@PathVariable String mark, Model model) {
        ChannelApkConfig apkConfig = channelApkService.selectApkConfig(mark);
        model.addAttribute("config", apkConfig);
        List<ApkSimulator> vers = apkSimulatorService.getVers(mark);
        model.addAttribute("sysVerList", vers);
        List<InterceptInfo> interceptList = channelConfigService.getInterceptInfoByChannelApkInfo(apkConfig);
        model.addAttribute("interceptList", interceptList);

        return "apkconfig/copyConfig";
    }

    @RequestMapping(value = "configList", params = "json")
    @ResponseBody
    public Map<String, Object> configPageList(ChannelApkConfig channelApkInfo) {
        String isDelete = channelApkInfo.getIsDelete();
        if (isDelete != null && isDelete.equals("-1")) {
            channelApkInfo.setIsDelete(null);
        }
        if (StringUtils.isBlank(channelApkInfo.getApkName())) {
            channelApkInfo.setApkName(null);
        }
        if (StringUtils.isBlank(channelApkInfo.getSysVer())) {
            channelApkInfo.setSysVer(null);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        String mark = channelApkInfo.getMark();
        if (StringUtils.isNotBlank(mark)) {
            String[] marks = mark.split(" ");
            List<String> keywords = new ArrayList<>();
            for (String keyword : marks) {
                keywords.add(keyword);
            }
            channelApkInfo.setKeywords(keywords);
        }
        List<ChannelApkConfig> list = channelApkService.getConfigList(channelApkInfo);
        list = apkTaskQueryService.queryApkConfigList(list);
        Integer total = channelApkService.getCountOfConfig(channelApkInfo);
        map.put("rows", list);
        map.put("total", total);
        return map;
    }

    @RequestMapping(value = "saveProvince", method = RequestMethod.POST)
    @SystemControllerLog(description = "省份保存")
    public void saveProvince(String mark, String openProvince, HttpServletResponse response) {
        try {
            apkProvinceService.updateProvinceByMark(mark, openProvince);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("保存省份出错！", e);
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "saveLoginType", method = RequestMethod.POST)
    @SystemControllerLog(description = "logintype保存")
    public void saveLoginType(ApkLoginTypeConfig loginConfig, HttpServletResponse response) {
        List<ApkLoginTypeConfig> loginList = new ArrayList<ApkLoginTypeConfig>();
        loginList.add(loginConfig);
        apkLoginTypeService.insertAndUpdateApkLoginTypeConfig(loginList);
        ResponseUtils.responseSuccess(response);
    }

    @RequestMapping(value = "batchSaveLoginType", method = RequestMethod.POST)
    @SystemControllerLog(description = "logintype保存")
    public void batchSaveLoginType(ApkLoginTypeConfig loginConfig, HttpServletResponse response) {
        String marks = loginConfig.getMarks();
        String[] markArr = marks.split(",");
        List<ApkLoginTypeConfig> loginList = new ArrayList<ApkLoginTypeConfig>();
        for (String mark : markArr) {
            //apkLoginTypeService.updateRateByMark(mark);

            ApkLoginTypeConfig altc = new ApkLoginTypeConfig();
            altc.setMark(mark);
            altc.setRate(loginConfig.getRate());
            altc.setLoginType(loginConfig.getLoginType());
            altc.setApkId(loginConfig.getApkId());
            altc.setBase(loginConfig.getBase());
            altc.setLoginName(loginConfig.getLoginName());
            altc.setLeftNum(loginConfig.getLeftNum());
            altc.setExecTotal(loginConfig.getExecTotal());
            altc.setPayRate(loginConfig.getPayRate());
            altc.setPayMin(loginConfig.getPayMin());
            altc.setPayMax(loginConfig.getPayMax());
            altc.setBizId(loginConfig.getBizId());
            altc.setMobile(loginConfig.getMobile());
            loginList.add(altc);
        }
        apkLoginTypeService.insertAndUpdateApkLoginTypeConfig(loginList);
        ResponseUtils.responseSuccess(response);
    }

    @RequestMapping(value = "saveActionType", method = RequestMethod.POST)
    @SystemControllerLog(description = "actiontype保存")
    public void saveActionType(RetentFilterConfig config, HttpServletResponse response) {
        retentFilterConfigService.insertAndUpdateRetentFilterConfig(config);
        ResponseUtils.responseSuccess(response);
    }

    @RequestMapping(value = "batchSaveActionType", method = RequestMethod.POST)
    @SystemControllerLog(description = "actiontype保存")
    public void batchSaveActionType(RetentFilterConfig config, HttpServletResponse response) {
        String ids = config.getIds();
        String[] idArr = ids.split(",");
        List<RetentFilterConfig> list = new ArrayList<RetentFilterConfig>();
        for (String id : idArr) {
            RetentFilterConfig rfc = new RetentFilterConfig();
            rfc.setApkId(Integer.valueOf(id));
            rfc.setActionType(config.getActionType());
            rfc.setEnable(config.getEnable());
            rfc.setName(config.getName());
            rfc.setPriority(config.getPriority());
            list.add(rfc);
        }
        retentFilterConfigService.batchInsertAndUpdateRetentFilterConfig(list);
        ResponseUtils.responseSuccess(response);
    }

    @RequestMapping(value = "saveDevice", method = RequestMethod.POST)
    @SystemControllerLog(description = "device保存")
    public void saveDevice(ApkDevice device, HttpServletResponse response) {
        Integer enable = device.getEnable();
        Integer apkId = device.getApkId();
        deviceService.insertApkDevice(device);
        List<ApkDevice> list = deviceService.selectDevices(apkId);
        boolean flag = false;
        if (list != null) {
            for (ApkDevice temp : list) {
                Integer tempEnable = temp.getEnable();
                if (tempEnable != null && tempEnable == 1) {
                    flag = true;
                    break;
                }
            }
        }
        ChannelApkConfig config = channelApkService.selectApkConfigByid(apkId);
        if (enable != null && enable == 1) {
            config.setIsAssignDevice(1);
        } else {
            if (flag) {
                config.setIsAssignDevice(1);
            } else {
                config.setIsAssignDevice(0);
            }
        }
        channelApkService.updateApkConfig(config);
        ResponseUtils.responseSuccess(response);
    }

    @RequestMapping(value = "{mark}/provinceList")
    public String provinceList(@PathVariable String mark, Model model) {
        List<ProvinceInfo> provinceList = ipService.getProvinceInfoList();
        List<ChannelProvinceInfo> markProvinceList = apkProvinceService.getConfigByMark(mark);
        Set<Integer> provinceSet = new HashSet<>();
        if (markProvinceList != null && markProvinceList.size() > 0) {
            for (ChannelProvinceInfo info : markProvinceList) {
                Integer procinceId = Integer.parseInt(info.getProvinceid());
                provinceSet.add(procinceId);
            }
        }
        if (provinceList != null && provinceList.size() > 0) {
            Iterator<ProvinceInfo> itetator = provinceList.iterator();
            while (itetator.hasNext()) {
                ProvinceInfo province = itetator.next();
                Integer provinceId = province.getId();
                if (provinceSet.contains(provinceId)) {
                    province.setChecked(true);
                } else {
                    province.setChecked(false);
                }
                if(provinceId== -1){
                    province.setProvinceName("未知");
                }
            }
        }
        model.addAttribute("provinceList", provinceList);
        return "apkconfig/addProvince";
    }

    @RequestMapping(value = "{mark}/loginList")
    public String loginList(@PathVariable String mark, Model model) {
        model.addAttribute("mark", mark);
        return "apkconfig/addLoginList";
    }

    @RequestMapping(value = "{marks}/batchLoginList")
    public String batchLoginList(@PathVariable String marks, Model model) {
        model.addAttribute("marks", marks);
        return "apkconfig/batchAddLoginList";
    }

    @RequestMapping(value = "{apkId}/retentFilterConfig")
    public String retentFilterConfig(@PathVariable Integer apkId, Model model) {
        model.addAttribute("apkId", apkId);
        return "apkconfig/retentFilterConfig";
    }

    @RequestMapping(value = "{ids}/batchEditRetent")
    public String batchEditRetent(@PathVariable String ids, Model model) {
        model.addAttribute("ids", ids);
        return "apkconfig/batchEditRetentPage";
    }

    @RequestMapping(value = "{apkId}/deviceConfig")
    public String deviceConfig(@PathVariable Integer apkId, Model model) {
        model.addAttribute("apkId", apkId);
        return "apkconfig/deviceConfig";
    }

    @RequestMapping(value = "{apkId}/updateDevice", params = "json")
    @ResponseBody
    public Map<String, List<ApkDevice>> updateDevice(@PathVariable Integer apkId, Model model) {
        Map<String, List<ApkDevice>> map = new HashMap<String, List<ApkDevice>>();
        List<ApkDevice> deviceList = deviceService.selectDevices(apkId);
        map.put("rows", deviceList);
        return map;
    }

    @RequestMapping(value = "{apkId}/updateRetentFilter", params = "json")
    @ResponseBody
    public Map<String, List<RetentFilterConfig>> updateRetentFilter(@PathVariable Integer apkId, Model model) {
        Map<String, List<RetentFilterConfig>> map = new HashMap<String, List<RetentFilterConfig>>();
        List<RetentFilterConfig> filterList = retentFilterConfigService.selectRetentFilterConfigList(apkId);
        map.put("rows", filterList);
        return map;
    }

    @RequestMapping(value = "updateRetentFilterBatch", params = "json")
    @ResponseBody
    public Map<String, List<RetentFilterConfig>> updateRetentFilterBatch(Model model) {
        Map<String, List<RetentFilterConfig>> map = new HashMap<String, List<RetentFilterConfig>>();
        List<RetentFilterConfig> filterList = retentFilterConfigService.selectALLRetentFilterConfigList();
        map.put("rows", filterList);
        return map;
    }

    @RequestMapping(value = "{mark}/updateLoginList", params = "json")
    @ResponseBody
    public Map<String, List<ApkLoginTypeConfig>> updateLoginList(@PathVariable String mark, Model model) {
        Map<String, List<ApkLoginTypeConfig>> map = new HashMap<String, List<ApkLoginTypeConfig>>();
        List<ApkLoginTypeConfig> loginList = channelApkService.selectApkLoginConfigList(mark);
        map.put("rows", loginList);
        return map;
    }

    @RequestMapping(value = "batchUpdateLoginList", params = "json")
    @ResponseBody
    public Map<String, List<ApkLoginTypeConfig>> batchUpdateLoginList(Model model) {
        Map<String, List<ApkLoginTypeConfig>> map = new HashMap<String, List<ApkLoginTypeConfig>>();
        List<ApkLoginTypeConfig> loginList = channelApkService.selectApkLoginConfigList();
        map.put("rows", loginList);
        return map;
    }

    @RequestMapping(value = "apkList")
    @ResponseBody
    public String selectApkList(String apk) {
        ChannelApkConfig config = new ApkRetentConfig();
        config.setMark(apk);
        return JSONObject.toJSONString(channelApkService.queryApkInfos(config));
    }

    // 导出查询列表
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ResponseBody
    public void export(HttpServletResponse response, ChannelApkConfig channelApkInfo, HttpServletRequest request) throws Exception {

        String isDelete = channelApkInfo.getIsDelete();
        if (isDelete != null && isDelete.equals("-1")) {
            channelApkInfo.setIsDelete(null);
        }
        if (StringUtils.isBlank(channelApkInfo.getApkName())) {
            channelApkInfo.setApkName(null);
        }
        if (StringUtils.isBlank(channelApkInfo.getSysVer())) {
            channelApkInfo.setSysVer(null);
        }

        Integer total = channelApkService.getCountOfConfig(channelApkInfo);

        JSONObject resultJson = new JSONObject();
        String size = PropertiesUtil.getValue("excel_max_size");
        if (total == 0) {
            resultJson.put(ReturnJsonCode.RETURN_CODE, ReturnJsonCode.MsgCodeEnum.EXCEL_EXPORT_ERROR.getCode());
            resultJson.put(ReturnJsonCode.RETURN_MSG, "操作失败！该范围内没有数据。");
            ResponseUtils.renderJson(response, resultJson.toString());
            return;
        }
        if (total > Integer.parseInt(size)) {
            resultJson.put(ReturnJsonCode.RETURN_CODE, ReturnJsonCode.MsgCodeEnum.EXCEL_EXPORT_ERROR.getCode());
            resultJson.put(ReturnJsonCode.RETURN_MSG, "操作失败！查询数据超过excel可导出上限。");
            ResponseUtils.renderJson(response, resultJson.toString());
            return;
        }
        channelApkInfo.setRows(total);
        List<ChannelApkConfig> list = channelApkService.getConfigList(channelApkInfo);
        list = apkTaskQueryService.queryApkConfigList(list);
        list = channelApkService.getExportMsg(list);
        response.setContentType("octets/stream");
        String filename = DateUtil.formatDate(new Date()) + "apk查询列表.xlsx";
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "iso8859-1"));

        ExportExcel<ChannelApkConfig> excel = new ExportExcel<ChannelApkConfig>();

        String[] headers = {"应用标识", "apk名称", "启用状态 ", "	新增任务量 ", "留存任务量 ", "基地", "渠道", "手机版本", "过滤文件","登录配置", "发送短信类型", "留存限制 ", "指定机型", "是否拉活", "是否补量", "是否留存顺序执行", "修改时间"};

        OutputStream out = response.getOutputStream();

        String title = "apk查询列表";

        excel.exportExcel(title, headers, list, out, "yyyy-MM-dd");

        out.close();
    }

    @RequestMapping(value = "batchEditVerSubmit", method = RequestMethod.POST)
    @SystemControllerLog(description = "更新配置")
    public void batchEditVerSubmit(String marks, String sysVer, HttpServletResponse response) {
        try {
            channelApkService.batchEditVerSubmit(marks, sysVer);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("批量修改版本号出错", e);
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "{marks}/batchEditFile")
    public String batchEditFile(@PathVariable String marks, Model model) {
        String mark = marks.split(",")[0];
        ChannelApkConfig apkConfig = channelApkService.selectApkConfig(mark);
        model.addAttribute("marks", marks);
        model.addAttribute("config", apkConfig);
        List<ApkSimulator> vers = apkSimulatorService.getVers(mark);
        model.addAttribute("sysVerList", vers);
        List<InterceptInfo> interceptList = channelConfigService.getInterceptInfoByChannelApkInfo(apkConfig);
        model.addAttribute("interceptList", interceptList);

        return "apkconfig/batchEditFile";
    }

    @RequestMapping(value = "batchEditFileSubmit", method = RequestMethod.POST)
    @SystemControllerLog(description = "更新配置")
    public void batchEditFileSubmit(String marks, @RequestParam MultipartFile[] file, HttpServletResponse response) {
        try {
            channelApkService.batchEditFileSubmit(marks, file);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("修改apk出错", e);
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "batchUpdateConfig", method = RequestMethod.POST)
    @SystemControllerLog(description = "更新配置")
    public void batchUpdateConfig(ChannelApkConfig apkConfig, @RequestParam MultipartFile[] file, HttpServletResponse response) {
        try {
            String marks = apkConfig.getMarks();
            String[] markArr = marks.split(",");
            for (String mark : markArr) {
                apkConfig.setMark(mark);
                channelApkService.batchUpdateApkConfig(file, apkConfig);
            }

            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("修改apk出错", e);
            ResponseUtils.responseFailure(response);
        }
    }
    
//    @RequestMapping(value = "{mark}/toUpgradeApk")
//    public String toUpgradeApk(@PathVariable String mark, Model model) {
//    	UpgradeApkBean uab =  upgradeApkService.getByMark(mark);
//    	if (null == uab) {
//    		uab = new UpgradeApkBean();
//    		uab.setMark(mark);
//    	}
//        model.addAttribute("uab", uab);
//        return "apkconfig/upgradeApk";
//    }
    
    @RequestMapping(value = "{ids}/toUpgradeApk")
    public String toUpgradeApk(@PathVariable String ids, Model model) {
    	model.addAttribute("ids", ids);
        return "apkconfig/upgradeApk";
    }
    
    @RequestMapping(value = "/upgradeApk", method = RequestMethod.POST)
    public void upgradeApk(String marketplace, Integer num, String ids,  @RequestParam MultipartFile file, HttpServletResponse response) {
        try {
        	if (file == null) {
        		 ResponseUtils.responseValidation(response, "file为空");
        	}
        	if (num == null) {
        		num = 0;
        	}
        	
        	FileUploadVo fileVo = upgradeApkService.uploadApk(file);
        	
        	String[] idArr = ids.split(",");
        	for (String id : idArr) {
        		ChannelApkConfig channelApkConfig = channelApkService.selectApkConfigByid(Integer.valueOf(id));
        		String mark = channelApkConfig.getMark();
        		
        		UpgradeApkBean uab = new UpgradeApkBean();
        		uab.setMark(mark);
        		uab.setMarketplace(marketplace);
        		uab.setNum(num);
        		uab.setVersion(fileVo.getVersion());
        		uab.setApkPath(fileVo.getFilePath());
        		upgradeApkService.upgradeApk(uab);
        		
        		redisClient.evalHset(CacheKey.UPGRADE_APK_INDEX, mark, String.valueOf(num));
            	redisClient.hset(CacheKey.UPGRADE_APK_DATA, mark, uab);
        	}
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("升级包出错", e);
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "{id}/toDelUpgradeApk")
    public String toDelUpgradeApk(@PathVariable String id, Model model) {
    	ChannelApkConfig channelApkConfig = channelApkService.selectApkConfigByid(Integer.valueOf(id));
		String mark = channelApkConfig.getMark();
		UpgradeApkBean uab = upgradeApkService.getByMark(mark);
		model.addAttribute("uab", uab);
    	model.addAttribute("id", id);
        return "apkconfig/delUpgradeApk";
    }
    
    @RequestMapping(value = "/delUpgradeApk", method = RequestMethod.POST)
    @ResponseBody
    public void upgradeApk(String id, HttpServletResponse response) {
        try {
        	if (id == null) {
        		 ResponseUtils.responseValidation(response, "id为空");
        	}
        	
    		ChannelApkConfig channelApkConfig = channelApkService.selectApkConfigByid(Integer.valueOf(id));
    		String mark = channelApkConfig.getMark();
    		int count = upgradeApkService.deleteByMark(mark);
    		if (count > 0) {
    			redisClient.evalHdel(CacheKey.UPGRADE_APK_INDEX, mark);
            	redisClient.hdel(CacheKey.UPGRADE_APK_DATA, mark);	
    		}
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("删除升级包出错", e);
            ResponseUtils.responseFailure(response);
        }
    }
    
    @RequestMapping(value = "batchSaveLoginTypeNew", method = RequestMethod.POST)
    @SystemControllerLog(description = "actiontype保存")
    public void batchSaveActionType2(String list, String marks, HttpServletResponse response) {
        List<ApkLoginTypeConfig> loginConfigs = JSONArray.parseArray(list, ApkLoginTypeConfig.class);
        if (CollectionUtils.isEmpty(loginConfigs)) {
        	return;
        }
        List<Integer> types = new ArrayList<>();
        for (ApkLoginTypeConfig apkLoginTypeConfig : loginConfigs) {
        	types.add(apkLoginTypeConfig.getLoginType());	
    	}
        String[] markArr = marks.split(",");
        for (String mark : markArr) {
        	for (ApkLoginTypeConfig apkLoginTypeConfig : loginConfigs) {
        		apkLoginTypeConfig.setMark(mark);
        	}
        	apkLoginTypeService.insertAndUpdateApkLoginTypeConfigNews(loginConfigs, types, mark);
        }
        
        ResponseUtils.responseSuccess(response);
    }
}
